package com.mxt.moviedemo.controller;


import com.mxt.moviedemo.entity.Actors;
import com.mxt.moviedemo.service.ActorsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("actor")
public class ActorController {
    private static final Logger logger = LoggerFactory.getLogger(ActorController.class);
    @Autowired
    private ActorsService  actorsService;
    @GetMapping("/searchActor")
    public Map<String, Object> searchActor(@RequestParam("aname") String aname) {
        logger.info("获取演员信息，id：{}", aname);
        Map<String, Object> result = new HashMap<>();

        try {
            List<Actors> actors=actorsService.searchActorsByName(aname);
            if (actors != null) {
                result.put("success", true);
                result.put("actors", actors);
            } else {
                result.put("success", false);
                result.put("error", "未找到该演员");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", "查询演员信息失败：" + e.getMessage());
        }
        return result;
    }
    @GetMapping("/infoActor")
    public Map<String, Object> getUserInfo(@RequestParam("aid") int aid) {
        logger.info("获取演员信息，id：{}", aid);
        Map<String, Object> result = new HashMap<>();

        try {
            Actors actors = actorsService.getActorById(aid);
            if (actors != null) {
                result.put("success", true);
                result.put("actors", actors);
            } else {
                result.put("success", false);
                result.put("error", "未找到该演员");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", "查询演员信息失败：" + e.getMessage());
        }
        return result;
    }

    // 新增订票
    @PostMapping("addActors")
    public Map<String, Object> addActors(@RequestBody Actors actors) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = actorsService.addActors(actors);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "演员信息添加成功");
            } else {
                result.put("success", false);
                result.put("message", "演员信息添加失败");
            }
        } catch (Exception e) {
            logger.error("添加演员信息错误: ", e);
            result.put("success", false);
            result.put("message", "演员信息添加失败：" + e.getMessage());
        }
        return result;
    }

    @PutMapping("updateActors")
    public Map<String, Object> updateActor(@RequestBody Actors actors) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (actorsService.updateActors(actors)) {
                result.put("success", true);
                result.put("message", "演员信息修改成功");
            } else {
                result.put("success", false);
                result.put("message", "演员信息修改失败");
            }
        } catch (Exception e) {
            logger.error("演员信息错误: ", e);
            result.put("success", false);
            result.put("message", "演员信息修改失败：" + e.getMessage());
        }
        return result;
    }
    @DeleteMapping("deleteActors/{aid}")
    public Map<String, Object> deleteTicket(@PathVariable int aid) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = actorsService.deleteActors(aid);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "演员信息删除成功");
            } else {
                result.put("success", false);
                result.put("message", "演员信息删除失败");
            }
        } catch (Exception e) {
            logger.error("删除演员信息错误: ", e);
            result.put("success", false);
            result.put("message", "演员信息删除失败：" + e.getMessage());
        }
        return result;
    }
}
